package controller

import (
	"strconv"

	"im/model"
	"im/requests"
	"im/service"
)

type InternalMails struct {
	Base
}

// swagger:route GET /systems/internal_mails systems get-internal-mails
//   获取站内信列表
// ////
//   获取站内信列表
//   Security:
//   - jwt_key:
//   Responses:
//     200: GetInternalMails
func (c *InternalMails) GetInternal_mails() {
	var user = c.Ctx.Values().Get("user").(model.Users)
	var param = requests.GetInternalMails{
		Pagination: requests.Pagination{
			Page:  1,
			Limit: 10,
		},
		ReadStatus: model.InternalMails_NotRead,
	}
	_ = c.Ctx.ReadQuery(&param)
	resp, err := service.NewInternalMails().List(user.Id, param)
	if err != nil {
		c.SendCry(err)
		return
	}
	c.SendSmile(resp)
}

// swagger:route Delete /systems/internal_mails/clear_read systems delete-internal-mails
//   清空已读信息
//
//   清空已读信息
//   Security:
//   - jwt_key:
//   Responses:
//     200:
func (c *InternalMails) DeleteInternal_mailsClear_read() {
	var user = c.Ctx.Values().Get("user").(model.Users)
	err := service.NewInternalMails().HideAllRead(user.Id)
	if err != nil {
		c.SendCry(err)
		return
	}
	c.SendSmile(nil)
}

// swagger:route PUT /systems/internal_mails/read_receipt/{id} systems put-internal-mails-read-receipt-by-id
//   已读回执,当id为all时表示全部已读
//
//   已读回执,当id为all时表示全部已读
//   Security:
//   - jwt_key:
//   Responses:
//     200:
func (c *InternalMails) PutInternal_mailsRead_receiptBy(idStr string) {
	var user = c.Ctx.Values().Get("user").(model.Users)
	var err error
	if idStr == "all" {
		err = service.NewInternalMails().ReadAllUnread(user.Id)
	} else {
		id, _ := strconv.ParseInt(idStr, 10, 64)
		err = service.NewInternalMails().ReadReceipt(user.Id, id)

	}
	if err != nil {
		c.SendCry(err)
		return
	}
	c.SendSmile(nil)
}
